package com.example.windowdemo.window.strategy;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;

import com.example.windowdemo.callback.WindowStrategy;

/**
 * @author: njb
 * @date: 2023/8/3 14:06
 * @desc:
 */
public class StrategyWindow <T>{
    private final Context context;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    public int width;
    public int height;
    private T data;
    private static final String TAG = "WindowLog";
    public WindowManager.LayoutParams layoutParams;
    public View contentView;
    private final WindowStrategy windowStrategy;

    public StrategyWindow(Context context, WindowStrategy windowStrategy) {
        this.context = context;
        this.windowStrategy = windowStrategy;
        this.layoutParams = new WindowManager.LayoutParams();
    }

    public void initView() {
        windowStrategy.initView(this);
    }

    public void setFullScreen(boolean isFullScreen) {
        windowStrategy.setFullScreen(this, isFullScreen);
        this.isFullScreen = isFullScreen;
    }

    public void setTitleBar(boolean hasTitleBar) {
        windowStrategy.setTitleBar(this, hasTitleBar);
        this.hasTitleBar = hasTitleBar;
    }

    public void setSize(int width, int height) {
        windowStrategy.setSize(this, width, height);
        this.width = width;
        this.height = height;
    }

    public void setPosition(int x, int y) {
        windowStrategy.setPosition(this, x, y);
    }

    public void setGravity(int gravity) {
        windowStrategy.setGravity(this, gravity);
    }

    public void setData(T data) {
        this.data = data;
        Log.d(TAG, "===获取到的数据为===" + data.toString());
    }

    public void addView(View view) {
        this.contentView = view;
    }

    protected boolean autoRegisterEventBus() {
        return false;
    }

    public boolean dispatchKeyEvent(KeyEvent keyEvent) {
        return false;
    }

    public void show() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(contentView, layoutParams);
    }

    public void hide() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.removeView(contentView);
    }

    public void onDetachedFromWindow() {
        // 视图分离
    }
}
